/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mlbs.session;

import com.mlbs.entity.Application;
import com.mlbs.entity.Pagecache;
import com.mlbs.entity.Preferences;
import com.mlbs.entity.Usersession;
import com.mlbs.entity.Usertable;
import com.mlbs.helper.CacheData;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Responsible for providing a centralized caching services for the middleware
 * Applications will be able to store and fetch data to and from the user cache based on the 
 * device. 
 * 
 * @author hubharan
 */
@PersistenceContext(name="mlbs-ejbPU", unitName="mlbs-ejbPU")
@Stateless
public class CachingSBBean implements CachingSBLocal {
    @PersistenceContext(unitName="mlbs-ejbPU")
    private EntityManager em;
    
    public EntityManager getEntityManager() {
        return em;
    }
    /**
     * Reterive the data from cache for a given user based on application id, 
     * device id, and the index of the cache. Cache is maintained as an array where 
     * length of array is 5. Per application stores up to 5 pages per device in
     * the cache data.
     * @param appId Application identification
     * @param deviceId Device identification
     * @param session session object
     * @param index index of cache array
     * @return CacheData object
     */
    public CacheData getCacheData(int appId, String deviceId, String session, int index) {
        Usersession sess = (Usersession)getEntityManager().
                createNamedQuery("Usersession.findBySession").
                setParameter("session", session).
                getSingleResult();
        Usertable user = (Usertable)getEntityManager().
                createNamedQuery("Usertable.findByUserId").
                setParameter("userId", sess.getId()).
                getSingleResult();  
        
        Application app = (Application)getEntityManager().
                createNamedQuery("Application.findByAppId").
                setParameter("appId", appId).
                getSingleResult();
        Collection<Preferences> pref = user.getPreferencesCollection();
        
        
        //Get App ID out of this and check if pref allows caching for given app
        //Collection<Pagecache> cache = ((Preferences)(pref.toArray()[0])).getPagecacheCollection();        
        
        
        return null;
    }

    /**
     * Set the cache data for a given device and application in the user cache.
     * @param appId application identification
     * @param deviceId Device identification
     * @param session session object returned by authentation
     * @param pageData Cache data that needs to be stored
     * @return true is success, else false
     */
    public boolean setUserCache(int appId, String deviceId, String session, CacheData pageData) {
        Usersession sess = (Usersession)getEntityManager().
                createNamedQuery("Usersession.findBySession").
                setParameter("session", session).
                getSingleResult();
        Usertable user = (Usertable)getEntityManager().
                createNamedQuery("Usertable.findByUserId").
                setParameter("userId", sess.getId()).
                getSingleResult();  
        
        Application app = (Application)getEntityManager().
                createNamedQuery("Application.findByAppId").
                setParameter("appId", appId).
                getSingleResult();
        
        return false;
    }

    /**
     * Clears the user cache for given application
     * @param appId application identification
     * @param session session object
     * @param deviceId Device identification
     * @return true is success, else false
     */
    public boolean clearCache(int appId, String session,  String deviceId) {
        Usersession sess = (Usersession)getEntityManager().
                createNamedQuery("Usersession.findBySession").
                setParameter("session", session).
                getSingleResult();
        Usertable user = (Usertable)getEntityManager().
                createNamedQuery("Usertable.findByUserId").
                setParameter("userId", sess.getId()).
                getSingleResult();  
        
        Application app = (Application)getEntityManager().
                createNamedQuery("Application.findByAppId").
                setParameter("appId", appId).
                getSingleResult();
        Collection<Preferences> pref = user.getPreferencesCollection();
        
        
        //Get App ID out of this and check if pref allows caching for given app
        Collection<Pagecache> cache = null;//((Preferences)(pref.toArray()[0])).getPagecacheCollection();
        em.remove((CacheData) cache);
        return true;
    }
    
    
    
    // Add business logic below. (Right-click in editor and choose
    // "EJB Methods > Add Business Method" or "Web Service > Add Operation")
 
    
    
    
}
